package repository

import (
	"gorm.io/gorm"
	"shop-bean/model"
)

type IFileRepository interface {
	FindExpireFile(*gorm.DB) ([]*model.File, error)
	DeleteBatch(db *gorm.DB, ids []uint) error
}

type FileRepository struct{}

func NewFileRepository() IFileRepository {
	return &FileRepository{}
}

// 3 正常, 2 过期, 1 用户上传附件24小时内都没使用该附件，该附件变为过期
func (r *FileRepository) FindExpireFile(db *gorm.DB) (files []*model.File, err error) {
	return files, db.Model(&model.File{}).
		Where("status = 1").
		Where("UNIX_TIMESTAMP( expire_at ) < UNIX_TIMESTAMP( NOW() )").
		Or("status = 2").
		Find(&files).Error
}

func (r *FileRepository) DeleteBatch(db *gorm.DB, ids []uint) error {
	return db.Where("id in ?", ids).Delete(&model.File{}).Error
}
